
TARGET = mm32_make_jlink
DEBUG = 1
OPT = -Og

ASM_SOURCES = startup_mm32f3270_gcc.s

PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
 
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S

BUILD_DIR = debug

C_INCLUDES =  \
-Idrv \
-Idrv/adc \
-Idrv/dac \
-Idrv/gpio \
-Idrv/systick \
-Idrv/uart \
-Ilib/CMSIS/include \
-Ilib/MM32F327x/HAL_Lib/Inc \
-Ilib/MM32F327x/Include 

##想编译的文件目录 
##手动输入：所有目录及子目录都需加上
#SRC_DIRS  = ../a ../a/aa ../a/bb ../b 
##递归遍历：3级
SRC_DIRS := $(shell find drv -maxdepth 3 -type d) \
			$(shell find lib -maxdepth 4 -type d) \
			$(shell find user -maxdepth 1 -type d)
##EX_FILES即想排除的文件

EX_FILES = lib/MM32F327x/HAL_Lib/Src/hal_can.c \
			lib/MM32F327x/HAL_Lib/Src/hal_can.c \
			lib/MM32F327x/HAL_Lib/Src/hal_comp.c \
			lib/MM32F327x/HAL_Lib/Src/hal_crc.c \
			lib/MM32F327x/HAL_Lib/Src/hal_crs.c \
			lib/MM32F327x/HAL_Lib/Src/hal_eth.c \
			lib/MM32F327x/HAL_Lib/Src/hal_exti.c \
			lib/MM32F327x/HAL_Lib/Src/hal_flash.c \
			lib/MM32F327x/HAL_Lib/Src/hal_fsmc.c \
			lib/MM32F327x/HAL_Lib/Src/hal_i2c.c \
			lib/MM32F327x/HAL_Lib/Src/hal_iwdg.c \
			lib/MM32F327x/HAL_Lib/Src/hal_pwr.c \
			lib/MM32F327x/HAL_Lib/Src/hal_rtc.c \
			lib/MM32F327x/HAL_Lib/Src/hal_sdio.c \
			lib/MM32F327x/HAL_Lib/Src/hal_spi.c \
			lib/MM32F327x/HAL_Lib/Src/hal_tim.c \
			lib/MM32F327x/HAL_Lib/Src/hal_uid.c \
			lib/MM32F327x/HAL_Lib/Src/hal_ver.c \
			lib/MM32F327x/HAL_Lib/Src/hal_wwdg.c \
#######################################
# CFLAGS
#######################################
CPU = -mcpu=cortex-m3
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
AS_DEFS = 
C_DEFS =  \
-DUSE_STDPERIPH_DRIVER
AS_INCLUDES = 
ASFLAGS = $(MCU) $(AS_DEFS) $(OPT) -Wall -fdata-sections -ffunction-sections

CFLAGS = $(MCU) $(C_DEFS) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif
LDSCRIPT = startup_mm32f3270_gcc.ld
LIBS = -lc -lm -lnosys 
LIBDIR = 
LDFLAGS = $(MCU) -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
##foreach 遍历想编译的源目录，wildcard 获取目录下所有文件并加上后缀.c（语法上是为.c加上前缀，前缀为目录下的文件名，即为文件加后缀）
SRC_FILES  = $(filter-out ${EX_FILES}, $(foreach dir, $(SRC_DIRS), $(wildcard $(addprefix $(dir)/*, .c))))

CFLAGS += $(C_INCLUDES)
#basename 返回一个字符串 . 之前的所有字段,即去掉后缀，同时addsuffix 加上后缀.o

OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(SRC_FILES:.c=.o)))
vpath %.c $(sort $(dir $(SRC_FILES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

#通配符 @:目标文件，^:所有的依赖文件，<:第一个依赖文件
#-c：生成xxx.o的目标文件 -o：生成可执行程序的目标文件



$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@	
	
$(BUILD_DIR):
	mkdir $@		


-include $(wildcard $(BUILD_DIR)/*.d)

#.PHONY跟随的代表虚拟目标，不需要clean文件存在
.PHONY: clean
clean:
	-rm -fR $(BUILD_DIR)
